home *** CD-ROM | disk | FTP | other *** search
Text File | 1989-02-07 | 9.6 KB | 347 lines | [TEXT/PJMM] |
- { ADBDemo }
- { ©1989 MacTutor}
- { By Dave Kelly, modified by David Smith}
- { This program will blink the LEDs of the Extended Keyboard on and off twice}
- { This has no real value other than to show how to communication with ADB devices}
- { NOTE: It is not clear what Apple has in mind for these LEDs, so you are using them at your own risk}
- { Refer to Technical Note #206 for more information.}
- { Other tidbits include Multifinder friendly, about dialog, vers resources, working sizable window}
- program ADBDemo;
-
- uses
- OSIntf, PrintTraps, MyADBGlobals, MyADBStuff;
-
- procedure crash;
- begin
- ExitToShell;
- end;
-
- procedure InitMac;
- begin
- InitGraf(@thePort);
- InitFonts;
- InitWindows;
- InitMenus;
- TEInit;
- InitDialogs(@crash);
- InitCursor;
- FlushEvents(everyEvent, 0);
- Finished := false;
- end;
-
- procedure initRects;
- var
- MemoryPtr: ^Integer;
- begin
- MemoryPtr := pointer(mBarHeightGlobal); {System Global mBarHeight}
- mBarHeight := MemoryPtr^;
- screen := ScreenBits.Bounds; {current screen device}
- SetRect(DragArea, Screen.left + 4, Screen.top + mBarHeight + 4, Screen.right - 4, Screen.bottom - 4);
- SetRect(GrowArea, Screen.left + MinWidth, Screen.top + MinHeight, Screen.right - 8, Screen.bottom - 8);
- SetRect(ADBWindowRect, Screen.left + 15, Screen.top + 45, Screen.right - 95, Screen.bottom - 95);
- SetRect(ZoomRect, Screen.left + 4, Screen.top + mBarHeight + 4, Screen.right - 4, Screen.bottom - 4);
- end;
-
- procedure InitMyWindow;
- var
- windtype: integer;
- Visible: boolean;
- GoAway: boolean;
- RefVal: LongInt;
- title: str255;
- begin
- Visible := true;
- windtype := documentProc + ZoomBox;
- GoAway := true;
- RefVal := 0;
- title := 'ADB Demo';
- ADBWindow := NewWindow(nil, ADBWindowRect, title, Visible, windtype, pointer(-1), GoAway, RefVal);
- ADBWindowPeek := WindowPeek(ADBWindow);
- SetPort(ADBWindow);
- TextFont(Geneva);
- TextSize(10);
- TextFace([]); {plain}
- TextMode(1); {Or}
- PenNormal;
- ForeColor(blackColor);
- BackColor(whiteColor);
- ADBWindowPeek^.windowKind := userKind;
- with ADBWindow^.portRect do
- begin
- SetRect(VCRect, right - (SBarWidth - 1), top - 1, right + 1, bottom - (SBarWidth - 2));
- SetRect(HCRect, left - 1, bottom - (SBarWidth - 1), right - (SBarWidth - 2), bottom + 1);
- SetRect(GrowRect, HCRect.right, HCRect.top, VCRect.right, HCRect.bottom);
- SetRect(ViewRect, left + 4, top + 4, right - (SBarWidth - 1), bottom - (SBarWidth - 1));
- end; {of with }
- DestRect := ViewRect;
- myTextHandle := TENew(DestRect, ViewRect);
- title := '';
- VControl := NewControl(ADBWindow, VCRect, title, Visible, 0, 0, 0, ScrollBarProc, 1);
- ValidRect(VCRect);
- HControl := NewControl(ADBWindow, HCRect, title, Visible, 0, 0, 0, ScrollBarProc, 1);
- ValidRect(HCRect);
- end;
-
- procedure InitMyPrint;
- begin
- myPrint := THPrint(NewHandle(SIZEOF(TPrint)));
- end;
-
- procedure InitMyMenus;
- var
- i: integer;
- begin
- myMenus[AppleM] := GetMenu(AppleMenu);
- AddResMenu(myMenus[AppleM], 'DRVR');
- myMenus[FileM] := GetMenu(FileMenu);
- myMenus[EditM] := GetMenu(EditMenu);
- for i := 1 to MenuCount do
- InsertMenu(myMenus[i], 0);
-
- DisableItem(myMenus[FileM], fSave);
- DisableItem(myMenus[FileM], fSaveAs);
- DisableItem(myMenus[FileM], fPageSet);
- DisableItem(myMenus[FileM], fPrint);
- DisableItem(myMenus[EditM], eUndo);
- DrawMenuBar;
- end; {of proc}
-
- procedure doMouse (myEvent: EventRecord);
- var
- whereIsIt: integer;
- whichWindow: WindowPtr;
- localPt, globalPt: Point;
- oldPort: GrafPtr;
- begin
- globalPt := myEvent.where;
- localPt := globalPt; {global coord of mouse}
- GlobalToLocal(localPt); {local coord of mouse}
- whereIsIt := FindWindow(globalPt, whichWindow);
- case whereIsIt of
- inDesk: {0}
- doMessage('Mouse Click on Desktop.', '(Not handled in this program.)', '', '');
- inMenuBar: {1}
- doMenuBar(MenuSelect(globalPt));
- inSysWindow: {2}
- SystemClick(myEvent, whichWindow);
- inContent: {3}
- doContent(myEvent, whichWindow);
- inDrag: {4}
- doDrag(whichWindow, globalPt);
- inGrow: {5}
- doGrow(whichWindow, globalPt, False);
- inGoAway: {6}
- if TrackGoAway(whichWindow, globalPt) then
- HideWindow(whichWindow);
- inZoomIn, InZoomOut: {7, 8}
- begin
- if TrackBox(whichWindow, globalPt, whereIsIt) then
- begin
- GetPort(OldPort);
- SetPort(whichWindow); {safety device}
- EraseRect(whichWindow^.portRect);
- ZoomWindow(whichWindow, whereIsIt, True);
- doGrow(whichWindow, globalPt, True);
- SetPort(OldPort);
- end;
- end;
- otherwise
- begin
- end;
- end; {of whereIsIt}
- end;
-
- procedure doKeyDowns (myEvent: EventRecord);
- var
- ch: char;
- charCode: longInt;
- keyCode: longInt;
- begin
- charCode := BitAnd(myEvent.Message, charCodeMask); {strip off key code}
- keyCode := BitShift(BitAnd(myEvent.Message, keyCodeMask), -8); {strip off char}
- ch := Chr(charCode); {get keyboard char}
- if BitAnd(myEvent.Modifiers, CmdKey) = CmdKey then
- doMenuBar(MenuKey(ch)) { do menu command key}
- else
- begin { do keystroke }
- ParamText('No typing supported…', '', '', '');
- itemhit := CautionAlert(AlertDialog, nil);
- end; { of do key stroke }
- end; { of proc}
-
- procedure doUpdates (myEvent: EventRecord);
- var
- UpdateWindow: WindowPtr;
- TempPort: GrafPtr;
- myRect: rect;
- begin
- UpdateWindow := WindowPtr(myEvent.message);
- if UpdateWindow = ADBWindow then
- begin
- GetPort(TempPort); {save port}
- SetPort(UpdateWindow);
- BeginUpDate(UpdateWindow);
- EraseRect(UpdateWindow^.portRect);
- DrawGrowIcon(UpdateWindow);
- DrawControls(UpdateWindow);
- TEUpdate(UpdateWindow^.portRect, myTextHandle);
- EndUpDate(UpdateWindow);
- SetPort(TempPort); {restore port}
- end;
- end; {of proc}
-
- procedure doActivates (myEvent: EventRecord);
- var
- TargetWindow: WindowPtr;
- TargetPeek: WindowPeek;
- begin
- TargetWindow := WindowPtr(myEvent.message);
- TargetPeek := windowPeek(TargetWindow);
- SetPort(TargetWindow);
- if Odd(myEvent.modifiers) then
- begin {activate}
- if TargetWindow = ADBWindow then
- begin
- DisableItem(myMenus[EditM], eUndo);
- DrawGrowIcon(TargetWindow);
- TEActivate(myTextHandle);
- ShowControl(VControl);
- ShowControl(HControl);
- end
- end { of activate loop}
- else
- begin {deactivate}
- if TargetWindow = ADBWindow then
- begin
- EnableItem(myMenus[EditM], eUndo);
- EnableItem(myMenus[EditM], eCut);
- EnableItem(myMenus[EditM], eCopy);
- EnableItem(myMenus[EditM], ePaste);
- EnableItem(myMenus[EditM], eClear);
- DrawGrowIcon(TargetWindow);
- TEDeactivate(myTextHandle);
- HideControl(VControl);
- HideControl(HControl);
- end; { of my window activation}
- end; {of deactivate loop}
- end; {of proc}
-
- procedure doMulti (myEvent: EventRecord);
-
- const
- MouseMovedEvt = $FA;
- var
- HiByte: byte;
- bit0: LongInt;
- sysresult: boolean;
- ResumeWindow: WindowPtr;
- ResumePeek: WindowPeek;
- SuspendWindow: WindowPtr;
- SuspendPeek: WindowPeek;
- MouseMove: LongAndByte;
- begin
- bit0 := 31; {convert 68000 to toolbox}
- {check for mouse moved event}
- MouseMove.longView := myEvent.message;
- HiByte := Byte(MouseMove.byteView.byte0);
- if HiByte = MouseMovedEvt then
- begin {Handle mouse moved event}
- end;
- {check for resume event }
- if Odd(myEvent.message) then
- begin {resume}
- { treat like activate Event if we are in front}
- ResumeWindow := FrontWindow;
- if ResumeWindow = ADBWindow then
- begin {adbWindow}
- SetPort(ResumeWindow);
- InvalRect(ResumeWindow^.portRect); { force update event}
- DisableItem(myMenus[EditM], eUndo);
- ShowControl(VControl);
- ShowControl(HControl);
- DrawGrowIcon(ResumeWindow);
- end; {adbWindow}
- if FrontWindow <> nil then
- begin {DA check}
- ResumePeek := WindowPeek(FrontWindow);
- if ResumePeek^.windowKind < 0 then {DA}
- begin {da}
- myEvent.what := activateEvt;
- BitSet(@myEvent.modifiers, bit0);
- sysresult := SystemEvent(myEvent);
- end; {da}
- end; {DA check}
- { end of activate Event}
- end {of resume}
- else
- begin {suspend}
- {de-activate Event}
- SuspendWindow := FrontWindow;
- if SuspendWindow = ADBWindow then
- begin {adbwindow}
- SetPort(SuspendWindow);
- InvalRect(SuspendWindow^.portRect); {force update}
- EnableItem(myMenus[EditM], eUndo);
- EnableItem(myMenus[EditM], eCut);
- EnableItem(myMenus[EditM], eCopy);
- EnableItem(myMenus[EditM], ePaste);
- EnableItem(myMenus[EditM], eClear);
- DrawGrowIcon(SuspendWindow);
- HideControl(VControl);
- HideControl(HControl);
- end; {adbwindow}
- if FrontWindow <> nil then
- begin {DA check}
- SuspendPeek := WindowPeek(FrontWindow);
- if SuspendPeek^.windowKind < 0 then
- begin {da}
- myEvent.what := activateEvt;
- BitClr(@myEvent.modifiers, bit0);
- sysresult := SystemEvent(myEvent);
- end; {da}
- end; {DA check}
- { end of de-activate Event}
- end; {suspend}
- end; {of proc}
-
- procedure MainEventLoop;
- const
- MultiFinderEvt = 15;
- var
- sleep: LongInt;
- Event: EventRecord;
- DoIt: Boolean;
- begin
- sleep := 10;
- repeat
- TEIdle(MyTextHandle);
- DoIt := WaitNextEvent(EveryEvent, Event, sleep, nil); {no mouse tracking}
- if DoIt then
- case Event.what of
- mouseDown:
- doMouse(Event);
- KeyDown, Autokey:
- doKeyDowns(Event);
- updateEvt:
- doUpdates(Event);
- activateEvt:
- doActivates(Event);
- MultiFinderEvt:
- doMulti(Event);
- otherwise
- begin
- end;
- end; {of event case}
- until Finished; {end program}
- end;
-
- begin { main }
-
- InitMac;
- InitRects;
- InitMyWindow;
- InitMyPrint;
- InitMyMenus;
- MainEventLoop;
-
- end. { end of main }